package cn.mesmile.design.single;

/**
 * @author zb
 * @date 2021/11/19 16:34
 * @Description
 */
public class SingleInstance {

    /**
     * volatile是Java提供的关键字，它具有可见性和有序性，
     *  指令重排序是JVM对语句执行的优化，只要语句间没有依赖，那JVM就有权对语句进行优化
     *  禁止了指令重排
     */
    private volatile static  SingleInstance SINGLE_INSTANCE;

    private SingleInstance(){}

    public static SingleInstance  getInstance(){
        if (SINGLE_INSTANCE == null) {
            synchronized (SingleInstance.class) {
                if (SINGLE_INSTANCE == null) {
                    SINGLE_INSTANCE = new SingleInstance();
                }
            }
        }
        return SINGLE_INSTANCE;
    }

}
